package ug.edu.ec.libs;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

	Properties objPropertiesConstants;

	public Properties getObjProperties() {
		return this.objPropertiesConstants;
	}

	public void setProperties(String path) {
		try {
			objPropertiesConstants = new Properties();
			objPropertiesConstants.load(getClass().getResourceAsStream(path));
			if (objPropertiesConstants.isEmpty()) {
				objPropertiesConstants = null;
			}
		} catch (IOException ex) {
			objPropertiesConstants = null;
		}
	}

	public String abrirArchivo(String vStrPath) throws Exception {
		FileInputStream fstream = new FileInputStream(vStrPath);
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader d = new BufferedReader(new InputStreamReader(in));
	String vStrFile="";
		while (d.ready()) {
			vStrFile+=d.readLine()+"\n";
		}

		in.close();
return vStrFile;
	}

	

	public String paddingText(String vStrText, String vStrOrient,
			String vStrCharacter, int vIntLength) throws Exception {
		String vStrResult = "";

		String fStrOrientLeft = objPropertiesConstants
				.getProperty("fStrOrientLeft");
		String fStrOrientRigth = objPropertiesConstants
				.getProperty("fStrOrientRigth");
		if (vStrOrient.equals(fStrOrientLeft)) {
			vStrResult = vStrText
					+ String.format(
							"%" + (vIntLength - vStrText.length()) + "s", "")
							.replace(" ", String.valueOf(vStrCharacter));
		} else {
			if (vStrOrient.equals(fStrOrientRigth)) {
				vStrResult = String.format(
						"%" + (vIntLength - vStrText.length()) + "s", "")
						.replace(" ", String.valueOf(vStrCharacter))
						+ vStrText;
			} else {
				vStrResult = vStrText;
			}
		}

		return vStrResult;
	}

	public boolean comparateString(String vStrOrigin, String vStrDestine) {
		boolean vBoolResult = false;
		vStrOrigin = vStrOrigin.toLowerCase().trim();
		vStrDestine = vStrDestine.toLowerCase().trim();
		if (vStrOrigin.equals(vStrDestine)) {
			vBoolResult = true;
		}
		return vBoolResult;
	}

	public ArrayList<Object> getArrayListObject(String vStrText,
			String vStrSeparator) {
		ArrayList<Object> arrayData = new ArrayList<>();

		StringTokenizer vObjTokens = new StringTokenizer(vStrText,
				vStrSeparator);

		while (vObjTokens.hasMoreTokens()) {
			String vStrParametro = vObjTokens.nextToken();
			if (vStrParametro.trim().equals(
					objPropertiesConstants.get("fStrNulo"))) {
				arrayData.add(null);
			} else {
				arrayData.add(vStrParametro);
			}

		}

		return arrayData;

	}

	public Timestamp getTodayTimestamp() {
		Calendar calendar = Calendar.getInstance();
		Timestamp timeStamp = new Timestamp(calendar.getTimeInMillis());
		return timeStamp;
	}

	public Timestamp getSumaMinutosTimestamp(int vIntMinutos) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, vIntMinutos);
		Timestamp timeStamp = new Timestamp(calendar.getTimeInMillis());
		return timeStamp;
	}

	public Timestamp getSumaHorasTimestamp(int vIntHoras) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR, vIntHoras);
		Timestamp timeStamp = new Timestamp(calendar.getTimeInMillis());
		return timeStamp;
	}

	public Timestamp getSumaDiasTimestamp(int vIntDias) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, vIntDias);
		Timestamp timeStamp = new Timestamp(calendar.getTimeInMillis());
		return timeStamp;
	}

	public String getDateNow(String vStrFormat) {
		Date vDateNow = new Date();
		SimpleDateFormat vSdfValue = new SimpleDateFormat(vStrFormat);
		return vSdfValue.format(vDateNow);
	}

	public String getHourNow(String vStrFormat) {
		Date vDateNow = new Date();
		SimpleDateFormat vSdfValue = new SimpleDateFormat(vStrFormat);
		return vSdfValue.format(vDateNow);
	}

	public java.sql.Date getDateAddDays(java.sql.Date vDateOriginDate,
			int vIntDias) {
		Calendar vCalNow = new GregorianCalendar();
		vCalNow.setTimeInMillis(vDateOriginDate.getTime());
		vCalNow.add(Calendar.DATE, vIntDias);
		return new java.sql.Date(vCalNow.getTimeInMillis());
	}

	public synchronized java.sql.Date getDateSubDays(
			java.sql.Date vDateOriginDate, int vIntDias) {
		Calendar vCalNow = new GregorianCalendar();
		vCalNow.setTimeInMillis(vDateOriginDate.getTime());
		vCalNow.add(Calendar.DATE, -vIntDias);
		return new java.sql.Date(vCalNow.getTimeInMillis());
	}

	public synchronized int getSubsDate(Date vDateInitDate,
			Date vDateOriginEndDate) {

		DateFormat vSdfValue = DateFormat.getDateInstance(DateFormat.MEDIUM);
		String fechaInicioString = vSdfValue.format(vDateInitDate);
		String fechaFinalString = vSdfValue.format(vDateOriginEndDate);
		try {
			vDateInitDate = vSdfValue.parse(fechaInicioString);
			vDateOriginEndDate = vSdfValue.parse(fechaFinalString);
		} catch (ParseException ex) {
		}

		long vLongInitialDateMs = vDateInitDate.getTime();
		long vLongEndDateMs = vDateOriginEndDate.getTime();
		long vLongSubs = vLongEndDateMs - vLongInitialDateMs;
		double vDoubleDays = Math.floor(vLongSubs / (1000 * 60 * 60 * 24));
		return ((int) vDoubleDays);
	}

	public synchronized java.util.Date getParseStringToDate(String vStrDate) {
		SimpleDateFormat vSdfValue = new SimpleDateFormat("dd-MM-yyyy");
		Date vDateOriginDateSend = null;
		try {
			vDateOriginDateSend = vSdfValue.parse(vStrDate);
			return vDateOriginDateSend;
		} catch (ParseException ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public String getParseDatetoString(Date vDate) {
		SimpleDateFormat dateformatddMMyyyy = new SimpleDateFormat(
				"dd/MM/yyyy HH:mm:ss");
		String reportDate = dateformatddMMyyyy.format(vDate);

		return reportDate;
	}

	public int getCalculateAge(Calendar vCalDateBorn) {

		Calendar vCalNow = Calendar.getInstance();
		int vIntAnios = vCalNow.get(Calendar.YEAR)
				- vCalDateBorn.get(Calendar.YEAR);
		int vIntMeses = vCalNow.get(Calendar.MONTH)
				- vCalDateBorn.get(Calendar.MONTH);
		int vIntDias = vCalNow.get(Calendar.DAY_OF_MONTH)
				- vCalDateBorn.get(Calendar.DAY_OF_MONTH);

		if (vIntMeses < 0 || (vIntMeses == 0 && vIntDias < 0)) {
			vIntAnios--;
		}
		return vIntAnios;
	}

	public String checkEmail(String vStrEmail) {
		String vStrData = "";
		String vStrRegex = "^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$";
		Pattern vPtPattern = Pattern.compile(vStrRegex);
		Matcher vMatMatcher = vPtPattern.matcher(vStrEmail);
		if (!vMatMatcher.matches()) {

			vStrData = objPropertiesConstants.getProperty(
					"fStrMessageErrorName").replace("@(content)", vStrEmail);

		}
		return vStrData;
	}

	public String checkName(String vStrName) {
		String vStrData = "";
		String vStrRegex = "[A-Z][a-zA-Z ]*\\D{3}";
		Pattern vPtPattern = Pattern.compile(vStrRegex);
		Matcher vMatMatcher = vPtPattern.matcher(vStrName);
		if (!vMatMatcher.matches()) {
			vStrData = objPropertiesConstants.getProperty(
					"fStrMessageErrorName").replace("@(content)", vStrName);
		}
		return vStrData;
	}

	public String checkPhone(String vStrNumber) {
		String vStrData = "";
		String vStrRegex = "[0-9]{9}";
		Pattern vPtPattern = Pattern.compile(vStrRegex);
		Matcher vMatMatcher = vPtPattern.matcher(vStrNumber);
		if (!vMatMatcher.matches()) {
			vStrData = objPropertiesConstants.getProperty(
					"fStrMessageErrorName").replace("@(content)", vStrNumber);
		}
		return vStrData;
	}

	public String getMessage(String vStrMessage, String vStrType) {
		return objPropertiesConstants.getProperty(vStrType).replace(
				"@(content)", vStrMessage);
	}

}
